//
//  JLContentViewController.m
//  JiuLe
//
//  Created by xcq on 15/12/22.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "AppDelegate.h"
#import "JlUserManager.h"
#import "LoginViewController.h"
#import "JLContentViewController.h"

#define kImgCount 3
@interface JLContentViewController ()<UIScrollViewDelegate>
{
    UIScrollView *scollView;
    UIButton *nextPage;
    UIPageControl *pageControl;
}

@end

@implementation JLContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpScrollView];
}

- (void)setUpScrollView {
    scollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scollView.pagingEnabled = YES;
    scollView.delegate = self;
    scollView.showsHorizontalScrollIndicator = NO;
    scollView.showsVerticalScrollIndicator = NO;
    scollView.contentSize = CGSizeMake([RectExtends getScreenFrameWithWidth] * 3, [RectExtends getScreenFrameWithHeight]);
    scollView.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:scollView];
    NSString *imgStr;
    if (iPhone4s) {
        imgStr = @"guide4s";
    } else {
        imgStr = @"guide";
    }
    for (int i = 0; i < kImgCount; i ++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:(CGRect){{i*[RectExtends getScreenFrameWithWidth],0},self.view.bounds.size}];
        NSString *imgName=[NSString stringWithFormat:@"%@%d.jpg",imgStr,i+1];
        img.image = [UIImage imageNamed:imgName];
        [scollView addSubview:img];
        if (i == kImgCount-1) {
            [self setUpBtn:img];
        }
    }
    
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake([RectExtends getScreenFrameWithWidth]/2-50, [RectExtends getScreenFrameWithHeight]-30, 100, 15)];
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.backgroundColor = [UIColor clearColor];
    pageControl.currentPage = 0;
    pageControl.numberOfPages = 3;
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
    [self.view bringSubviewToFront:pageControl];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = sender.frame.size.width;
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)setUpBtn:(UIImageView *)img {
    img.userInteractionEnabled = YES;
    [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(joinNextPage)]];
}

- (void)joinNextPage {
    [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:JiuLeNewFeatures];
    [[NSUserDefaults standardUserDefaults]setObject:@(NO) forKey:JiuLeFirstRun];
    [AppDelegate chooseRootViewController];
}

- (void)changePage:(UIPageControl *)page {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
